package webServiceClient.academico;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class FrmPontuacao extends Activity {
	private Spinner cbDisciplina, cbTurma, cbAtividade;
	private Button btnClose, btnOk, btnLoadAtv, btnLoadTurma;
	private EditText edtAnoTurma, edtCodAluno;
	private ListView lista;

	private void carregaComboAtv() {
		String[] codAtividade;
		try {
			WebServiceClient ws = new WebServiceClient();
			codAtividade = ws.getAtividade("getAtividades", Definicoes
					.getRaAdm(), Definicoes.getSenhaAdm(), Definicoes
					.getSessao(cbDisciplina.getSelectedItem().toString(), "|",
							1));
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codAtividade);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbAtividade.setAdapter(adp);
			adp.notifyDataSetChanged();
		} catch (Exception e) {
			Toast t = Toast.makeText(this,
					"Erro ao preencher listas - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregaComboTurma() {
		String[] codDisciplina;
		try {
			WebServiceClient ws = new WebServiceClient();
			codDisciplina = ws.getTurmasProfessorAno("getTurmasProfessorAno",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm(),
					Integer.parseInt(edtAnoTurma.getText().toString()));
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codDisciplina);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbTurma.setAdapter(adp);
			adp.notifyDataSetChanged();
		} catch (Exception e) {
			Toast t = Toast.makeText(this, "Erro na lista - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregaCombo() {
		String[] codDisciplina;
		try {
			WebServiceClient ws = new WebServiceClient();
			codDisciplina = ws.getDisciplinaProfessor("getDisciplinaProfessor",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm());
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codDisciplina);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbDisciplina.setAdapter(adp);
		} catch (Exception e) {
			Toast t = Toast.makeText(this, "Erro na lista - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregaBtn() {
		cbDisciplina = (Spinner) findViewById(R.FrmAtribuicao.cbDisciplina);
		cbTurma = (Spinner) findViewById(R.FrmAtribuicao.cbTurma);
		cbAtividade = (Spinner) findViewById(R.FrmAtribuicao.cbAtividade);
		btnClose = (Button) findViewById(R.FrmAtribuicao.btnClose);
		btnOk = (Button) findViewById(R.FrmAtribuicao.btnOk);
		btnLoadAtv = (Button) findViewById(R.FrmPontuacao.btnLoadAtv);
		btnLoadTurma = (Button) findViewById(R.FrmPontuacao.btnLoadTurma);
		edtAnoTurma = (EditText) findViewById(R.FrmAtribuicao.edtAnoTurma);
		edtCodAluno = (EditText) findViewById(R.FrmAtribuicao.edtRaAluno);
		lista = (ListView) findViewById(R.FrmPontuacao.listpontos);

		btnLoadTurma.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				carregaComboTurma();
			}
		});
		btnLoadAtv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				carregaComboAtv();
			}
		});
		btnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					String[] codPontos;
					WebServiceClient ws = new WebServiceClient();
					codPontos = ws.getPontuacao("getPuntuacaoAluno", Definicoes
							.getRaAdm(), Definicoes.getSenhaAdm(), Long
							.parseLong(edtCodAluno.getText().toString()),
							Definicoes.getSessao(cbDisciplina.getSelectedItem()
									.toString(), "|", 1), Definicoes.getSessao(
									cbTurma.getSelectedItem().toString(), "|",
									1), Integer.parseInt(Definicoes.getSessao(
									cbTurma.getSelectedItem().toString(), "|",
									2)), Definicoes.getSessao(cbAtividade
									.getSelectedItem().toString(), "|", 1));

					ArrayList<String> list = new ArrayList<String>();
					for (int i = 0; i < codPontos.length; i++) {
						list.add(codPontos[i]);
					}

					StableArrayAdapter adapter = new StableArrayAdapter(
							FrmPontuacao.this,
							android.R.layout.simple_list_item_1, list);
					lista.setAdapter(adapter);
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmPontuacao.this,
							"Erro na lista - " + e.getMessage(),
							Toast.LENGTH_LONG);
					t.show();
					return;
				}
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_pontuacao);
		carregaBtn();
		carregaCombo();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_pontuacao, menu);
		return true;
	}

}
